﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;

namespace Sparklings.Gui
{
    interface ChoiceListElement
    {
        string GetText();
    }
    class ChoiceList : DraggableAndClickableControl
    {
        public ChoiceList(SpriteFont font, List<ChoiceListElement> list, int elementSize, Rectangle boundingBox)
            : base(boundingBox, new Vector2(0,elementSize * list.Count))
        {
            Font = font;
            Elements = list;
            LineHeight = elementSize;
            BoundingBox = boundingBox;
        }
        protected override void HandleClick(Vector2 clickPos)
        {
            int index = (int)(clickPos.Y / LineHeight);
            if (index >= 0 && index < Elements.Count)
            {
                OnSelect(Elements[index]);
            }
        }
        public Texture2D GetDrawn(GraphicsDevice graphicsDevice)
        {
            var batch = new SpriteBatch(graphicsDevice);
            var renderTarget = new RenderTarget2D(graphicsDevice, BoundingBox.Width, BoundingBox.Height);
            graphicsDevice.SetRenderTarget(renderTarget);
            graphicsDevice.Clear(Color.DarkBlue);
            batch.Begin();
            for (int i = 0; i < Elements.Count; ++i)
            {
                batch.DrawString(Font, Elements[i].GetText(), new Vector2(0, i * LineHeight) + ViewOffset, Color.White);
            }
            batch.End();
            graphicsDevice.SetRenderTarget(null);
            return renderTarget;
        }
        public Action<ChoiceListElement> OnSelect;
        private List<ChoiceListElement> Elements;
        private int LineHeight;
        private Rectangle BoundingBox;
        private SpriteFont Font;

    }
}
